package com.etc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.etc.dao.OrderDetailDao;
import com.etc.dao.PersonDao;
import com.etc.dao.ProductInfoDao;
import com.etc.entity.OrderInfo;
import com.etc.entity.Person;
import com.etc.entity.ProductInfo;
import com.etc.util.JdbcUtil;

/**
 * @author Administrator
 * 
 */
public class OrderDetailDaoImpl implements OrderDetailDao {

	/**
	 * 
	 * 方法作用:添加每个商品到订单中	
	 * 
	 * @param
	 * @return
	 * @throws SQLException 
	 */
	public void addEachProduct(Connection conn,int oid, int pid, int quantity, double total) throws SQLException {
		String sql = "insert into orderdetail (oid,opid,quantity,total) values (?,?,?,?)";
		JdbcUtil.doUpate(conn, sql, oid,pid,quantity,total);
		
	}


	/**
	 * 
	 * 方法作用:通过订单号,查看购买的所有商品信息
	 * 
	 * @param
	 * @return
	 */
	public List<ProductInfo> findProductByOrderId(int oid) {
		List<ProductInfo> list = new ArrayList<ProductInfo>();
		Connection conn = JdbcUtil.getConn();
		ResultSet rs = null;
		String sql = "select * from join productInfo on orderdetail where oid=?";
		try {
			rs=JdbcUtil.doQuery(conn, sql, oid);
			while (rs.next()) {
				ProductInfo productInfo = new ProductInfo();
				productInfo.setId(rs.getInt("pid"));
				productInfo.setName(rs.getString("pname"));
				productInfo.setPrice(rs.getDouble("pprice"));
				productInfo.setStock(rs.getInt("stock"));
				productInfo.setImg(rs.getString("img"));
				list.add(productInfo);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;

	}

	@Override
	public void downOrder(Connection conn,int oid,int uid,double total,String time) throws SQLException {
		//总的订单 ： oid uid 总价total time 
		
		String sql="insert orders (oid,uid,total,time) values (?,?,?,?)";
		JdbcUtil.doUpate(conn, sql, oid,uid,total,time);
	}
	/**
	 * 检查订单id是否已经存在
	 */
	public boolean checkOid(Connection conn,int oid){
		ResultSet rs=null;
		boolean tag=false;
		String sql="select oid from orders where oid = ?";
		rs=JdbcUtil.doQuery(conn, sql, oid);
		try {
			if(rs.next()){
				tag=true;
			}else{
				 tag=false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tag;
	}
	


		

}
